package san.service.m;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.utils.Page;
import san.dao.m.OrderDao;
import san.dao.m.ProductDao;
import san.entity.m.Order;
import san.entity.m.Product;

@Service
@Transactional
public class OrderService {
	/**
	 * 自动生成OrderService类.
	 */
	private static final long serialVersionUID = -2672225383773235810L;
	@Autowired
	private OrderDao orderDao;
	@Autowired
	private ProductDao productDao;

	@Transactional(readOnly = true)
	public Page<Order> getAll(Page<Order> page) throws Exception {
		return orderDao.getAll(page);
	}

	@Transactional(readOnly = true)
	public Order get(String id) throws Exception {
		return orderDao.get(id);
	}

	public void save(List<Order> orders, Order order) throws Exception {
		Product product;
		for (Order o : orders) {
			if (o.getAmount() != null && o.getAmount() > 0) {
				Order od = new Order();
				od.setFromName(order.getFromName());
				od.setFromAddress(order.getFromAddress());
				od.setFromTelphone(order.getFromTelphone());
				od.setAmount(o.getAmount());
				product = productDao.get(o.getId());
				product.addOrder(od);
				productDao.save(product);
			}
		}
	}

	public void saveOrder(Order order) throws Exception {
		orderDao.save(order);
	}

	public void delete(String id) throws Exception {
		orderDao.delete(id);
	}

}
